package com.shigu.admin.manager.actions;


import com.opentae.data.mall.beans.RolePermission;
import com.opentae.data.mall.beans.UserRole;
import com.opentae.data.mall.interfaces.ManagerPermissionMapper;
import com.opentae.data.mall.interfaces.ManagerRoleMapper;
import com.shigu.admin.manager.common.JSONResult;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * action基础类
 * Created by zhaohongbo on 16/5/12.
 */
@Component
public class BaseAction {

    @Autowired
    private ManagerRoleMapper managerRoleMapper;

    @Autowired
    private ManagerPermissionMapper managerPermissionMapper;

    protected JSONResult obj;


    protected static final String ERROR500 = "common/500";
    protected static final String ERROR403 = "common/403";

    protected  Integer leftflag;

    public Integer getLeftflag() {
        if(leftflag==null){
            leftflag=0;
        }
        return leftflag;
    }

    public void setLeftflag(Integer leftflag) {
        this.leftflag = leftflag;
    }

    public JSONResult getObj() {
        return obj;
    }

    public void setObj(JSONResult obj) {
        this.obj = obj;
    }

    /**
     * shiro代理session
     * @return
     */
    public Session getShiroSession(){
        Session session = SecurityUtils.getSubject().getSession();
        return session;
    }

    /**
     * 清除用户SESSION
     */
    public void removeShiroSession(){
        if (SecurityUtils.getSubject().getSession() != null) {
            SecurityUtils.getSubject().getSession().removeAttribute(SessionEnum.LOGIN_USER.toString());
        }
    }


    /**
     * 会话用户对象
     * @return
     */
    public AuthorityUser getAuthorithUser(){
        Session session = getShiroSession();
        AuthorityUser authUser = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        return authUser;
    }

    /**
     * 登录用户名称
     * @return
     */
    public String getLoginName(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getUserName();
    }
    /**
     * 登录用户名称
     * @return
     */
    public String getUserName(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getNick();
    }

    /**
     * 获取用户权限, 仅仅二三级权限表，
     */
    public Map<RolePermission, List<RolePermission>> getPermissions(Object param) {
        List<UserRole> roles=managerRoleMapper.selRoleNamesByuserIds(getAuthorithUser().getUserId().toString());
        List<RolePermission> allPermissions=new ArrayList<>();
        for(UserRole ur:roles){
            allPermissions.addAll(managerPermissionMapper.selPermissiontagsByroleTag(ur.getRoleTag()));
        }
        Map<RolePermission, List<RolePermission>> rolePermissionMap = new LinkedHashMap<>();
        Long parent = null;
        Long permissId = null;
        if (param != null) {
            if (param instanceof Long) {
                parent = ((Long) param);
            } else if (param instanceof String) {
                try {
                    parent = Long.valueOf(param.toString());
                } catch (Exception ignored) {
                    for (RolePermission permission : allPermissions) {
                        if (permission.getPermissionTag().equals(param.toString())) {
                            parent = permission.getParentPid();
                            permissId = permission.getPermissionId();
                            break;
                        }
                    }
                }
            } else if (param instanceof RolePermission) {
                parent = ((RolePermission) param).getPermissionId();
            }
        }
        List<RolePermission> rolePermissions = getSubPermission(allPermissions, parent);
        for (RolePermission role : rolePermissions) {
            if (permissId != null && !Objects.equals(permissId, role.getPermissionId()))
                continue;
            List<RolePermission> thisRoleRolePermission = getSubPermission(allPermissions, role.getPermissionId());
            for (RolePermission permission : thisRoleRolePermission) {
                Long pid = permission.getPermissionId();
                List<RolePermission> rolePermissionList = rolePermissionMap.get(permission);
                if (rolePermissionList == null) {
                    rolePermissionList = new ArrayList<>();
                    rolePermissionMap.put(permission, rolePermissionList);
                }
                rolePermissionList.addAll(getSubPermission(allPermissions, pid));
            }
        }
        return rolePermissionMap;
    }

    public List<RolePermission> getSubPermission(List<RolePermission> permissions, Long parent) {
        List<RolePermission> target = new ArrayList<>();
        for (RolePermission permission : permissions) {
            if (Objects.equals(permission.getParentPid(), parent)) {
                target.add(permission);
            }
        }
        return target;
    }


}
